package hluck.me.srorage.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hluck.me.srorage.sys.common.Constast;
import hluck.me.srorage.sys.common.DataGridView;
import hluck.me.srorage.sys.common.ResultObj;
import hluck.me.srorage.sys.common.TreeNode;
import hluck.me.srorage.sys.domain.SysPermission;
import hluck.me.srorage.sys.service.SysPermissionService;
import hluck.me.srorage.sys.vo.SysPermissionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 老雷
 * @since 2021-02-15
 */
@RestController
@RequestMapping("/permission")
public class SysPermissionController {
    @Autowired
    private SysPermissionService permissionService;

    /**权限管理开始**/

    /**
     * 加载权限管理左边的树的json
     */
    @RequestMapping("loadPermissionManagerLeftTreeJson")
    public DataGridView loadDeptManagerLeftTreeJson(SysPermissionVo permissionVo){
        QueryWrapper<SysPermission> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("type", Constast.TYPE_MENU);
        List<SysPermission> list = this.permissionService.list(queryWrapper);
        ArrayList<TreeNode> treeNodes = new ArrayList<>();
        for (SysPermission permission : list) {
            Boolean spread=permission.getOpen()==1?true:false;
            treeNodes.add(new TreeNode(permission.getId(),permission.getPid(),permission.getTitle(),spread));
        }
        return new DataGridView(treeNodes);
    }
    /**
     * 全查询
     */
    @RequestMapping("loadAllPermission")
    public DataGridView loadAllPermission(SysPermissionVo permissionVo){
        IPage<SysPermission> page=new Page<>(permissionVo.getPage(),permissionVo.getLimit());

        QueryWrapper<SysPermission> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("type",Constast.TYPE_PERMISSION);
        queryWrapper.like(StringUtils.isNotBlank(permissionVo.getTitle()),"title",permissionVo.getTitle());
        queryWrapper.like(StringUtils.isNotBlank(permissionVo.getPercode()),"percode",permissionVo.getPercode());
        queryWrapper.eq(permissionVo.getId()!=null,"pid",permissionVo.getId());
        queryWrapper.orderByAsc("ordernum");

        this.permissionService.page(page,queryWrapper);

        return new DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     * 添加权限
     */
    @RequestMapping("addPermission")
    public ResultObj addPermission(SysPermissionVo permissionVo){
        try {
            permissionVo.setType(Constast.TYPE_PERMISSION);
            this.permissionService.save(permissionVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }
    /**
     * 修改权限
     */
    @RequestMapping("updatePermission")
    public ResultObj updatePermission(SysPermissionVo permissionVo){
        try {
            this.permissionService.updateById(permissionVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }


    /**
     * 加载最大的排序码
     */
    @RequestMapping("loadMaxPermissionOrderNum")
    public Map<String,Object> loadMaxOrderNum(){
        HashMap<String, Object> map = new HashMap<>();

        QueryWrapper<SysPermission> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("ordernum");
        List<SysPermission> list = permissionService.list(queryWrapper);
        if (list.size()>0){
            map.put("value",list.get(0).getOrdernum()+1);
        }else {
            map.put("value",1);
        }

        return map;
    }

    /**
     * 删除权限
     */
    @RequestMapping("deletePermission")
    public ResultObj deletePermission(SysPermissionVo permissionVo){
        try {
            this.permissionService.removeById(permissionVo.getId());
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
    /***权限管理结束**/
}

